package com.dsd.datav.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsd.datav.constants.CountAndBalance;
import com.dsd.datav.constants.DataTurn;
import com.dsd.datav.domain.Datavsmap;
import com.dsd.datav.dto.SelectData;
import com.dsd.datav.dto.SortData;
import com.dsd.datav.enums.IdicatorColor;
import com.dsd.datav.service.DatavsmapService;
import com.dsd.datav.mapper.DatavsmapMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class DatavsmapServiceImpl extends ServiceImpl<DatavsmapMapper, Datavsmap>
    implements DatavsmapService{
    @Autowired
    private DatavsmapMapper datavsmapMapper;

    @Override
    public List<Datavsmap> queryAll() {
        List<Datavsmap> datavsmaps = datavsmapMapper.queryAll();
        List<Datavsmap> color = getColor(datavsmaps);
        return  config(color);
    }

    @Override
    public List<Datavsmap> findAllByType(String type,String orgId) {
        List<Datavsmap> allByType = datavsmapMapper.findAllByType(type,orgId);
        List<Datavsmap> color =getColor(allByType);
        return config(color);
    }
    public List<SortData> sortFindAllByOrgIdANDType(String orgId, String type){
        return sum(orgId, type);
    }
    public List<SortData> sum(String orgId, String type){
        Integer count1=0;
        Integer count2=0;
        BigDecimal sum1 = BigDecimal.valueOf(0);
        BigDecimal sum2 = BigDecimal.valueOf(0);
        List<SortData> sortData =null;
        SortData sortData1 = null;
        List<Datavsmap> datavsmaps =datavsmapMapper.queryByOrgId(orgId);
            for (Datavsmap datavsmap : datavsmaps) {
                if (orgId.equals("6361")||orgId.equals("6368")){
                    List<Datavsmap> allByVillage = datavsmapMapper.findAllByVillage(datavsmap.getVillage());
                    sum1.add(datavsmap.getLoanBalanceDay());
                    sum2.add(datavsmap.getDepositDay());
                    sortData1.setName(datavsmap.getVillage());

            }else if (orgId.equals("6359")){

                }
            }
        sortData1.sethPayCount(count1);
        sortData1.setCreditCardCount(count2);
        sortData1.setLoanBalanceSum(sum1);
        sortData1.setDepositSum(sum2);
        sortData.add(sortData1);
        return sortData;
    }
    @Override
    public List<Datavsmap> findAllByName(String name) {
        List<Datavsmap> allByName = datavsmapMapper.findAllByName(name);
        return config(allByName);
    }
    @Override
    public List<Datavsmap> findAllByVillage(String village) {
        List<Datavsmap> datavsmaps = datavsmapMapper.findAllByVillage(village);
        return config(datavsmaps);
    }

    @Override
    public List<Datavsmap> queryByOrgId(String orgId) {
        List<Datavsmap> datavsmaps =datavsmapMapper.queryByOrgId(orgId);
        List<Datavsmap> color = getColor(datavsmaps);
        return config(color);
    }

    @Override
    public List<SortData> queryByOrgIdCount(String orgId) {
       return datavsmapMapper.queryByOrgIdCount(orgId);
    }

    @Override
    public List<SortData> queryCount() {
        return datavsmapMapper.queryCount();
    }

    @Override
    public List<SelectData> selectVillage(String orgId) {
        return datavsmapMapper.selectVillage(orgId);
    }

    @Override
    public List<SelectData> selectOrgName() {
        return datavsmapMapper.selectOrgName();
    }

    @Override
    public List<Datavsmap> findForm(Datavsmap datavsmap) {
        return datavsmapMapper.findForm(datavsmap);
    }

    /**
     * 封装类型转换
     * @param datavsmaps
     * @return
     */
    public  List<Datavsmap> config(List<Datavsmap> datavsmaps){
        List<Datavsmap> datavsmap = new ArrayList();
        DataTurn dataTurn = null;
        for (Datavsmap datavsmap1 : datavsmaps) {
            dataTurn = DataTurn.enumsToData(datavsmap1.getType(),datavsmap1.getIsCreditCard(),datavsmap1.getIsHpay());
            datavsmap1.setType(dataTurn.getLabel());
            datavsmap1.setIsCreditCard(dataTurn.getDesc());
            datavsmap1.setIsHpay(dataTurn.getDesc1());
            datavsmap.add(datavsmap1);
        }
        return datavsmap;
    }

    /**
     * 根据指标获取颜色
     * @param datavsmapList
     * @return
     */
    public List<Datavsmap> getColor(List<Datavsmap> datavsmapList){
        List<Datavsmap> datavsmap = new ArrayList();
        for (Datavsmap datavsmap1:datavsmapList){
            //存信惠
            if (datavsmap1.getIsCreditCard().equals("1")&& datavsmap1.getIsHpay().equals("1")&&getCompareTo(datavsmap1.getDepositDay())){
                datavsmap1.setColor(IdicatorColor.GREEN.color());
            //贷信惠
            }else if (datavsmap1.getIsCreditCard().equals("1")&& datavsmap1.getIsHpay().equals("1")&&getCompareTo(datavsmap1.getLoanBalanceDay())) {
                datavsmap1.setColor(IdicatorColor.GREEN.color());
            //存贷信
            }else if (getCompareTo(datavsmap1.getDepositDay())&&getCompareTo(datavsmap1.getLoanBalanceDay())&&datavsmap1.getIsCreditCard().equals("1")){
                datavsmap1.setColor(IdicatorColor.GREEN.color());
            //存贷惠
            }else if (getCompareTo(datavsmap1.getDepositDay())&&getCompareTo(datavsmap1.getLoanBalanceDay())&&datavsmap1.getIsHpay().equals("1")){
                datavsmap1.setColor(IdicatorColor.GREEN.color());
            //存贷
            }else if (getCompareTo(datavsmap1.getDepositDay())&&getCompareTo(datavsmap1.getLoanBalanceDay())){
                datavsmap1.setColor(IdicatorColor.YELLOW.color());
            //存信
            }else if (getCompareTo(datavsmap1.getDepositDay())&&datavsmap1.getIsCreditCard().equals("1")){
                datavsmap1.setColor(IdicatorColor.YELLOW.color());
            //存惠
            }else if (getCompareTo(datavsmap1.getDepositDay())&&datavsmap1.getIsHpay().equals("1")){
                datavsmap1.setColor(IdicatorColor.YELLOW.color());
            //贷信
            }else if (getCompareTo(datavsmap1.getLoanBalanceDay())&&datavsmap1.getIsCreditCard().equals("1")){
                datavsmap1.setColor(IdicatorColor.YELLOW.color());
            //贷惠
            }else if (getCompareTo(datavsmap1.getLoanBalanceDay())&&datavsmap1.getIsHpay().equals("1")){
                datavsmap1.setColor(IdicatorColor.YELLOW.color());
            //其余全部为Red
            }else{
                datavsmap1.setColor(IdicatorColor.RED.color());
            }
            datavsmap.add(datavsmap1);
        }
        return datavsmap;
    }

    /**
     * decimal比较贷款日均；存款日均。
     * @param bigDecimal
     * @return
     */
    public boolean getCompareTo(BigDecimal bigDecimal){
        BigDecimal bigDecimal1 = new BigDecimal(10000);
        if (bigDecimal.compareTo(bigDecimal1) >= 0){
            return true;
        }else {
            return false;
        }
    }
}




